package sz.szsmk.citizencard.ui.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusSearchLineViewBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.mvp.contract.IQueryLineByNameContact;
import sz.szsmk.citizencard.mvp.presenter.QueryLineByLineNamePresenter;
import sz.szsmk.citizencard.ui.adapter.BusNearbyLineAdapter;
import sz.szsmk.citizencard.ui.adapter.BusSiteNearbyAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/19 17:14
 * description:公交搜索线路
 */
@Route("BusSearchLineActivity")
public class BusSearchLineActivity extends IBaseActivity<QueryLineByLineNamePresenter, BusSearchLineViewBinding>
        implements IQueryLineByNameContact.IQueryLineByNameView,
        View.OnClickListener {


    ImageView back;
    TextView settingTitle;
    TextView bjTv;
    EditText searchEt;
    private BusNearbyLineAdapter busNearbyLineAdapter;
    private String data;
    private BusSiteNearbyAdapter busSiteNearbyAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_search_line_view;
    }

    @Override
    protected void initInjector() {

        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        back = findViewById(R.id.back);
        settingTitle= findViewById(R.id.setting_title);
        bjTv= findViewById(R.id.bj_tv);
        searchEt= findViewById(R.id.search_et);

        dataBinding.titleLayout.settingTitle.setText("搜索");
        data = getIntent().getStringExtra("data");

        //判断关键词：包含数字 则查询线路，不包含 查询站点
        boolean isNum = HasDigit(data);
        if (isNum) {
            busNearbyLineAdapter = new BusNearbyLineAdapter(BusSearchLineActivity.this);
            dataBinding.busSearchLineRv.setLayoutManager(new LinearLayoutManager(this));
            dataBinding.busSearchLineRv.setAdapter(busNearbyLineAdapter);
            mPresenter.queryLineByLineName(data);
        } else {
            dataBinding.busSearchLineRv.setAdapter(busNearbyLineAdapter);
            busSiteNearbyAdapter = new BusSiteNearbyAdapter(this);
            dataBinding.busSearchLineRv.setAdapter(busSiteNearbyAdapter);
            mPresenter.queryBusStandByKey(data);
        }

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                boolean isNum = HasDigit(data);
                if (isNum) {
                    mPresenter.queryBusStandByKey(data);
                } else {
                    //线路查询
                    mPresenter.queryLineByLineName(data);
                }
            }
        });

        dataBinding.titleLayout.back.setOnClickListener(v -> finish());
    }

    // 判断一个字符串是否含有数字
    public boolean HasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void queryLineByLineNameFail(QueryBusLineNearbyRes response) {

    }

    @Override
    public void queryLineByLineNameSucc(QueryBusLineNearbyRes res) {

        ArrayList<QueryBusLineNearbyRes.BusLineArrayListBean> mList = new ArrayList<>();


        List<QueryBusLineNearbyRes.BusLineListBean> busLineList = res.getBusLineList();

        for (int i = 0; i < busLineList.size(); i++) {
            //接口获取
            QueryBusLineNearbyRes.BusLineListBean busLineListBean = busLineList.get(i);

            //new 一个要塞入的
            QueryBusLineNearbyRes.BusLineArrayListBean listBean = new QueryBusLineNearbyRes.BusLineArrayListBean();

            ArrayList<QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean> busLineListBeans = new ArrayList<>();
            QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean testBean = new QueryBusLineNearbyRes.BusLineArrayListBean.BusLineListBean();
            testBean.setLine_name(busLineListBean.getLine_name());
            testBean.setLine_guid(busLineListBean.getLine_guid());
            testBean.setLine_type(busLineListBean.getLine_type());
            testBean.setLine_direction(busLineListBean.getLine_direction());
            busLineListBeans.add(testBean);
            listBean.setBusLineList(busLineListBeans);
            listBean.setLine_name(busLineListBean.getLine_name());
            mList.add(listBean);
        }
        String json = new Gson().toJson(busLineList);

        busNearbyLineAdapter.setViewData(mList);
        busNearbyLineAdapter.notifyDataSetChanged();

        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

    @Override
    public void queryBusStandByKeySucc(List<QueryNearStandbyRes.BusSiteListBean> busSiteList) {

        busSiteNearbyAdapter.setViewData(busSiteList);
        busSiteNearbyAdapter.notifyDataSetChanged();
        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

    @Override
    public void queryBusStandByKeyFail() {

        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

}
